<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>资源管理</title>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/metro-blue/easyui.css">
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/icon.css">
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/static/ckeditor/ckeditor.js"></script>
    <script type="text/javascript">

        function searchArticle() {
            $("#dg").datagrid('load', {
                "name": $("#s_name").val(),
                "articleTypeId": $("#s_articleTypeId").val(),
                "userId": $("#s_userId").combogrid("getValue"),
                "state": $("#s_state").val(),
                "isUseful": $("#s_isUseful").val()
            });
        }

        function deleteArticle() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的资源!");
                return;
            }
            var strIds = [];
            for (var i = 0; i < selectedRows.length; i++) {
                strIds.push(selectedRows[i].id);
            }
            var ids = strIds.join(",");
            $.messager.confirm("系统提示", "你确定要删除这<span style='color: red'>" + selectedRows.length + "</span>篇资源吗?", function (r) {
                if (r) {
                    $.post("/admin/article/delete", {ids: ids}, function (result) {
                        if (result.success) {
                            $.messager.alert("系统提示", "数据已成功删除！");
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", "数据删除失败！");
                        }
                    }, "json");
                }
            });
        }

        function openArticleModifyTab() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个要查看的资源!");
                return;
            }
            var row = selectedRows[0];
            $("#dlg").dialog("open").dialog("setTitle", "查看资源");
            $("#fm").form("load", row);
            CKEDITOR.instances.content.setData(row.content);
            url = "/admin/article/save?id=" + row.id;
        }

        function submitData() {
            $("#fm").form("submit", {
                url: url,
                onSubmit: function () {
                    var content = CKEDITOR.instances.content.getData();
                    if (content == "") {
                        $.messager.alert("系统提示", "内容不能为空!");
                        return false;
                    }
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "保存成功!");
                        $("#dg").datagrid("reload");
                        $("#dlg").dialog("close");
                        resetValue();
                    } else {
                        $.messager.alert("系统提示", "保存失败！");
                    }
                }
            })
        }

        function formatType(val, row) {
            return val.name;
        }

        function closeArticleDialog() {
            $("#dlg").dialog("close");
            $("#dg").datagrid("reload");
            resetValue();
        }

        function resetValue() {
            $("#s_name").val("");
            $("#s_userId").combogrid("setValue", "");
            $("#s_articleTypeId").combobox("setValue", "");
            $("#s_state").combobox("setValue", "");
            $("#s_isUseful").combobox("setValue", "");
        }

        $(document).ready(function () {

            $("#dg").datagrid({
                onDblClickRow: function (index, row) {
                    $("#dlg").dialog("open").dialog("setTitle", "查看资源");
                    $("#fm").form("load", row);
                    CKEDITOR.instances.content.setData(row.content);
                    url = "/admin/article/save?id=" + row.id;
                }
            });
            $("#articleTypeId").combobox({
                mode: 'remote',
                url: '/admin/articleType/comboList',
                valueField: 'id',
                textField: 'name',
                delay: 100
            });
            $("#s_articleTypeId").combobox({
                mode: 'remote',
                url: '/admin/articleType/comboList',
                valueField: 'id',
                textField: 'name',
                delay: 100
            });
        });

        function pass() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个要审核通过的资源!");
                return;
            }
            var articleId = selectedRows[0].id;
            $.messager.confirm("系统提示", "确定将这个资源设置为<span style='color: red'>审核通过</span>吗?", function (r) {
                if (r) {
                    $.post("/admin/article/pass", {articleId: articleId}, function (result) {
                        if (result.success) {
                            $("#dg").datagrid("reload");
                            $.messager.alert("系统提示", "该资源的状态已经设置为<span style='color: red'>审核通过</span>!");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    })
                }
            });
        }

        function fail() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个要审核不通过的资源!");
                return;
            }
            var articleId = selectedRows[0].id;
            $("#dlg2").dialog("open").dialog("setTitle", "填写审核不通过的理由");
            $("#failArticleId").val(articleId);
        }

        function closeFailReasonDialog() {
            $("#dlg2").dialog("close");
        }

        function submitFailReason() {
            var id = $("#failArticleId").val();
            var reason = $("#failReason").val();
            $.messager.confirm("系统提示", "确定将这个资源设置为<span style='color: red'>审核不通过</span>吗?", function (r) {
                if (r) {
                    $.post("/admin/article/fail", {articleId: id, reason: reason}, function (result) {
                        if (result.success) {
                            closeFailReasonDialog();
                            $("#dg").datagrid("reload");
                            $.messager.alert("系统提示", "该资源状态已经设置为<span style='color: red'>审核不通过!</span>");
                            $("#failReason").val("");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    })
                }
            });
        }

        function modifyIsHot() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个资源!");
                return;
            }
            var articleId = selectedRows[0].id;
            var isHot = selectedRows[0].isHot;
            if (isHot == 0) {
                var str = "热门";
            } else {
                var str = "不热门";
            }
            $.messager.confirm("系统提示", "确定将这个资源设置为<span style='color: red'>" + str + "</span>吗?", function (r) {
                if (r) {
                    $.post("/admin/article/modifyIsHot", {articleId: articleId}, function (result) {
                        if (result.success) {
                            closeFailReasonDialog();
                            $("#dg").datagrid("reload");
                            $.messager.alert("系统提示", "该资源已经设置为<span style='color: red'>" + str + "</span>");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    })
                }
            });
        }

        function modifyIsOff() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length != 1) {
                $.messager.alert("系统提示", "请选择一个资源!");
                return;
            }
            var articleId = selectedRows[0].id;
            var isUseful = selectedRows[0].isUseful;
            if (isUseful == 0) {
                var str = "有效";
            } else {
                var str = "无效";
            }
            $.messager.confirm("系统提示", "确定将这个资源设置为<span style='color: red'>" + str + "</span>吗?", function (r) {
                if (r) {
                    $.post("/admin/article/modifyIsOff", {articleId: articleId}, function (result) {
                        if (result.success) {
                            closeFailReasonDialog();
                            $("#dg").datagrid("reload");
                            $.messager.alert("系统提示", "该资源已经设置为<span style='color: red'>" + str + "</span>");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    })
                }
            });
        }

        function formatState(val, row) {
            if (val == 1) {
                return "<span style='color: blue'>未审核</span>";
            } else if (val == 2) {
                return "<span style='color: green'>审核通过</span>";
            } else {
                return "<span style='color: red' title='" + row.reason + "'>审核不通过</span>";
            }
        }

        function formatUser(val, row) {
            return val.userName;
        }

        function formatIsHot(val, row) {
            if (val == 0) {
                return "<span style='color: red'>否</span>";
            } else {
                return "<span style='color: green'>是</span>";
            }
        }

        function formatIsUseful(val, row) {
            if (val == 0) {
                return "<span style='color: red'>否</span>";
            } else {
                return "<span style='color: green'>是</span>";
            }
        }

        function formatName(val, row) {
            if (val.length > 15) {
                return "<span title='"+val+"'>"+val.substring(0, 15)+"</span>...";
            } else {
                return val;
            }
        }
    </script>
</head>
<body style="margin: 1px">
<table id="dg" title="资源管理" class="easyui-datagrid" striped="true"
       fitColumns="true" pagination="true" rownumbers="true"
       url="/admin/article/list" fit="true" toolbar="#tb">
    <thead>
    <tr>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="20" align="center" hidden="true">编号</th>
        <th field="publishDate" width="90" align="center">发布日期</th>
        <th field="articleType" width="100" align="center" formatter="formatType">资源类型</th>
        <th field="user" width="50" align="center" formatter="formatUser">发布人用户名</th>
        <th field="state" width="50" align="center" formatter="formatState">当前状态</th>
        <th field="points" width="50" align="center">下载所需积分</th>
        <th field="isHot" width="50" align="center" formatter="formatIsHot">是否热门</th>
        <th field="isUseful" width="50" align="center" formatter="formatIsUseful">是否有效</th>
        <th field="name" width="160" align="center" formatter="formatName">资源名称</th>
    </tr>
    </thead>
</table>
<div id="tb">
    <div>
        <a href="javascript:openArticleModifyTab()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">查看</a>
        <a href="javascript:deleteArticle()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
        <a href="javascript:pass()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">审核通过</a>
        <a href="javascript:fail()" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">审核不通过</a>
        <a href="javascript:modifyIsHot()" class="easyui-linkbutton" iconCls="icon-isHot" plain="true">设置热门与否</a>
        <a href="javascript:modifyIsOff()" class="easyui-linkbutton" iconCls="icon-isUseful" plain="true">设置有效与否</a>
        <span style="color: red">(鼠标停在审核不通过上面可以看到不通过的原因)</span>
    </div>
    <div>
        &nbsp;名称:&nbsp;<input type="text" id="s_name" style="width:140px"
                              onkeydown="if(window.event.keyCode===13) searchArticle()"/>
        &nbsp;用户名:&nbsp;
        <select id="s_userId" name="userId" class="easyui-combogrid" style="width:140px" data-options="
			panelWidth: 800,
			idField: 'id',
			textField: 'userName',
			url: '/admin/user/comboList',
			method: 'get',
			columns: [[
				{field:'id',title:'编号',width:40},
				{field:'userName',title:'用户名',width:60},
				{field:'nickName',title:'昵称',width:60,align:'right'},
				{field:'roleName',title:'身份',width:60,align:'right'},
				{field:'email',title:'邮箱',width:100},
			]],
			fitColumns: true
		">
        </select>
        &nbsp;资源类型:&nbsp;<input class="easyui-combobox" id="s_articleTypeId" name="articleTypeId" style="width: 140px"
                                editable="true"
                                data-options="panelHeight:'auto',valueField:'id',textField:'name',url:'/admin/articleType/comboList'"/>
        &nbsp;审核状态：&nbsp;
        <select id="s_state" class="easyui-combobox" name="type" style="width:100px;" panelHeight="auto">
            <option value="">请选择...</option>
            <option value="1">未审核</option>
            <option value="2">审核通过</option>
            <option value="3">审核不通过</option>
        </select>
        &nbsp;是否有效：&nbsp;
        <select id="s_isUseful" class="easyui-combobox" name="type" style="width:100px;" panelHeight="auto">
            <option value="">请选择...</option>
            <option value="1">有效</option>
            <option value="0">无效</option>
        </select>
        <a href="javascript:searchArticle()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
        <a href="javascript:resetValue()" class="easyui-linkbutton" iconCls="icon-reset" plain="true">重置</a>
    </div>
</div>
<div id="dlg" class="easyui-dialog" style="width: 1200px;height: 520px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
    <form action="" id="fm" method="post" enctype="multipart/form-data">
        <table cellspacing="20px">
            <tr>
                <td width="80px">资源标题：</td>
                <td>
                    <input type="text" id="name" name="name" style="width: 400px" required="true"
                           class="easyui-validatebox"/>
                </td>
            </tr>
            <tr>
                <td>所属类别：</td>
                <td>
                    <input class="easyui-combobox" id="articleTypeId" name="articleTypeId" style="width: 140px"
                           required="true" editable="false"
                           data-options="required:true,panelHeight:'auto',valueField:'id',textField:'name',url:'/admin/articleType/comboList'"/>
                </td>
            </tr>
            <tr>
                <td valign="top">资源内容：</td>
                <td>
                    <textarea id="content" name="content" rows="30" cols="80"></textarea>
                </td>
            </tr>
            <tr>
                <td width="80px">链接地址：</td>
                <td>
                    <input type="text" id="downloadLink" name="downloadLink" style="width: 400px" required="true"
                           class="easyui-validatebox"/>
                </td>
            </tr>
            <tr>
                <td width="80px">分享密码：</td>
                <td>
                    <input type="text" id="password" name="password" style="width: 400px" required="true"
                           class="easyui-validatebox"/>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:closeArticleDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
<div id="dlg2" class="easyui-dialog" style="width: 450px;height: 311px;padding: 10px 20px"
     closed="true" buttons="#dlg2-buttons" data-options="onClose:function(){resetValue()}">
    <form action="" id="fm" method="post" enctype="multipart/form-data">
        <table cellspacing="20px">
            <tr>
                <td valign="top">理由：</td>
                <td>
                    <input type="hidden" id="failArticleId">
                    <textarea id="failReason" rows="10" cols="35"></textarea>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg2-buttons">
    <a href="javascript:submitFailReason()" class="easyui-linkbutton" iconCls="icon-ok">提交</a>
    <a href="javascript:closeFailReasonDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
<script type="text/javascript">
    CKEDITOR.replace("content");
</script>
</body>
</html>